/*************************************************************
  Estimation of TIMING OF EVENTS MODELS admin removal:
**************************************************************/
global savepath\\Ssb2f\ssb26\SATSET\Immigranten\GBWD\CHOICES
set more off
set logtype text

*** Covariaten ***
global lft "lft1825 lft2530 lft3540 lft4045 lft4550 lft5055 lft5560 lft6065"
global Burgst "gehuwd samenwonend gescheiden"
global woning "Koop WOZ100 WOZ200 WOZ300 WOZ400"
global ink "mndink_neg mndink_k mndink_1000 mndink_3000 mndink_4000 mndink_5000 mndink_g"
global SBI "SBIland SBIindus SBIbouw SBIhor SBIhand SBIver SBIfin SBIzak SBIond SBIzorg SBIcult"
global Origin "interethnic NLparent otherherkomst"

global cogroup "EU newEU DC LDC"
*** SET ST-VARIABELEN ***
global pcMig "6 12 18 24 60"
global pcWerk "3 6 12 24 60"
global pcGWerk "3 6 12 18 24 36"

foreach co of global cogroup { 
 use $savepath\Arbeid3mnd9907, clear
 run $pathimmpgm\mak_countries
 keep if `co'==1
 run $savepath\Adjust_3mos
 run $savepath\Mak_tellers
 run $savepath\set_STSET
 gen byte admin = (eventMig==2)*d_mig*(SECa1==9)
 /*** INFO ON PREVIOUS STATE ***/
 run $savepath\Mak_Previous_var

 *** Covariaten ***
 run $savepath\mak_SSBvar

 run $savepath\Add_natUNemprate
 replace NatUnemrate =  NatUnemrate - 2.94
 sort rin datum, stable
 by rin: gen Unementry = NatUnemrate[1]
 global UNrate "NatUnemrate Unementry"

 log using $pathimmpgm\ToEadmin_`co'.txt, replace
 /*************************************************************
  Timing of events (with admin removal): 
  ONLY FIRST MIGRATION !
 *************************************************************/
 global ID rin
 run $savepath\prgToEadmin
 replace d_mig=d_mig*(admin==0)

 /*** create year dummies **/
 capture drop eerste
 by rin: gen eerste = (_n==1)
 by rin: gen byte year2000 = sum( (year(datum)==2000)*eerste)
 by rin: gen byte year2001 = sum( (year(datum)==2001)*eerste)
 by rin: gen byte year2002 = sum( (year(datum)==2002)*eerste)
 by rin: gen byte year2003 = sum( (year(datum)==2003)*eerste)
 by rin: gen byte year2004 = sum( (year(datum)==2004)*eerste)
 by rin: gen byte year2005 = sum( (year(datum)==2005)*eerste)
 by rin: gen byte year2006 = sum( (year(datum)==2006)*eerste)
 by rin: gen byte year2007 = sum( (year(datum)==2007)*eerste)

 global entryyr "year2000 year2001 year2002 year2003 year2004 year2005 year2006 year2007"
/*************************************************************
      CONSTANT (CAUSAL) EFFECTS
*************************************************************/
 ml model d1 TOEadmin2 ///
  (beta1: t0_mig t_mig d_mig t0_werk t_werk d_werk indwerk ///
          t0_Gwerk t_Gwerk d_Gwerk indGwerk admin = ///
        zelf Onbenefit Female $Burgst AANTK $lft $ink ///
        $SBI $woning $gebland $Origin $entryyr $UNrate intmig_*) ///
  (beta2: zelf Female $Burgst AANTK $lft $ink $SBI $woning repwerk ///
       $gebland $Origin $entryyr $UNrate intwerk_*) ///
  (beta3: Onbenefit Female $Burgst AANTK $lft $ink $woning repGwerk ///
       $gebland $Origin $entryyr $UNrate intGwerk_*) ///
  /gam2 /gam3 ///
  /a11 /a21 /a22 /a31 /a32 ///
  /logitp1 /logitp2, tech(bfgs)
 ml init bstart, copy
 ml max, diff
 matrix bToEadmin`co'= e(b)
 estimates store bToEadmin`co', title(ToE)
 estwrite * using $pathimmpgm\ToEadmin, replace

 /*************************************************************
      TIME VARYING (CAUSAL) EFFECTS
 *************************************************************/
 /*** causal effect after 1 year **/
 gen byte intwerkg_2 = intwerk_2
 gen byte intwerkg_3 = intwerk_3
 gen byte intwerkg_4 = intwerk_4 + intwerk_5 + intwerk_6
 gen byte intGwerkg_2 = intGwerk_2
 gen byte intGwerkg_3 = intGwerk_3
 gen byte intGwerkg_4 = intGwerk_4+intGwerk_5+intGwerk_6+intGwerk_7

 ml model d1 TOEadmin2 ///
  (beta1: t0_mig t_mig d_mig t0_werk t_werk d_werk indwerk ///
          t0_Gwerk t_Gwerk d_Gwerk indGwerk admin = ///
        zelf Onbenefit Female $Burgst AANTK $lft $ink ///
        $SBI $woning $gebland $Origin $entryyr $UNrate intmig_*) ///
  (beta2: zelf Female $Burgst AANTK $lft $ink $SBI $woning repwerk ///
       $gebland $Origin $entryyr $UNrate intwerk_*) ///
  (beta3: Onbenefit Female $Burgst AANTK $lft $ink $woning repGwerk ///
       $gebland $Origin $entryyr $UNrate intGwerk_*) ///
  (gam2: intwerkg_* ) (gam3: intGwerkg_* ) ///
  /a11 /a21 /a22 /a31 /a32 ///
  /logitp1 /logitp2, tech(bfgs)
 ml init bstart, copy
 ml max, diff
 matrix bToEadmin`co'tv = e(b)
 estimates store bToEadmin`co'tv, title(ToE tv)
 estwrite * using $pathimmpgm\ToEadmin, replace

 /**************************************************************
        TIME VARYING (CAUSAL) EFFECT
          + EFFECT DEPENDS ON COVARIATES
 ***************************************************************/
 capture drop intwerkg_2 intwerkg_3 intwerkg_4
 capture drop intGwerkg_2 intGwerkg_3 intGwerkg_4 

 gen byte intwerkg_2 = intwerk_2
 gen byte intwerkg_3 = intwerk_3
 gen byte intwerkg_4 = intwerk_4 + intwerk_5 + intwerk_6
 gen byte intGwerkg_2 = intGwerk_2
 gen byte intGwerkg_3 = intGwerk_3
 gen byte intGwerkg_4 = intGwerk_4+intGwerk_5+intGwerk_6+intGwerk_7
 global prevreemp "repreemp numreemp prevonben_werk prevUNdur1m_werk"
 global prevreemp "$prevreemp prevUNdur2m_werk prevUNdur3m_werk"
 global prevreemp "$prevreemp prevUNdur6m1j_werk prevUNdur1j_werk"
 global prevunemp "repGwerk numunemp prevEmpdur3m_Gwerk"
 global prevunemp "$prevunemp prevEmpdur6m_Gwerk prevEmpdur1j_Gwerk"

 ml model d1 TOEadmin2 ///
  (beta1: t0_mig t_mig d_mig t0_werk t_werk d_werk indwerk ///
          t0_Gwerk t_Gwerk d_Gwerk indGwerk admin = ///
        zelf Onbenefit Female $Burgst AANTK $lft $ink ///
        $SBI $woning $gebland $Origin $entryyr $UNrate intmig_*) ///
  (beta2: zelf Female $Burgst AANTK $lft $ink $SBI $woning repwerk ///
       $gebland $Origin $entryyr $UNrate intwerk_*) ///
  (beta3: Onbenefit Female $Burgst AANTK $lft $ink $woning repGwerk ///
       $gebland $Origin $entryyr $UNrate intGwerk_*) ///
  (gam2: Female gehuwd $prevreemp $entryyr intwerkg_* ) /// 
  (gam3: Female gehuwd AANTK $lft $prevunemp $entryyr intGwerkg_* ) ///
  /a11 /a21 /a22 /a31 /a32 ///
  /logitp1 /logitp2 , tech(bfgs)
 ml init bstart, copy
 ml max, diff
 matrix bToEadmin`co'tvvar = e(b)
 estimates store bToEadmin`co'tvvar, title(ToE tvvar)
 estwrite * using $pathimmpgm\ToEadmin, replace

 log close
} /* end cogroup loop */
